package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.YanchuSession;
import com.ticket.system.domain.vo.SessionVo;
import com.ticket.system.service.IYanchuSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 场次Controller
 * 
 * @date 2023-09-26
 */
@Api(description = "场次管理")
@RestController
@RequestMapping("/system/session")
public class YanchuSessionController extends BaseController
{
    @Autowired
    private IYanchuSessionService yanchuSessionService;


    @ApiOperation("查询场次列表")
    @PreAuthorize("@ss.hasPermi('system:session:list')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "performId",value = "演出主键",required = true,dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sessionName",value = "场次名称",required = false,dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "status",value = "状态筛选 0：创建中；1：待销售；2：暂停销售；3：取消；4：销售中；5：销售结束",required = false,dataType = "int"),

    })
    @GetMapping("/list")
    public TableDataInfo list(YanchuSession yanchuSession)
    {
        startPage();
        List<YanchuSession> list = yanchuSessionService.selectYanchuSessionBy(yanchuSession);
        return getDataTable(list);
    }

    @ApiOperation("小程序端查询场次（售票端）")
    @ApiImplicitParam(name = "performId",value = "演出id",required = true,dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/listVo/forAllUser")
    public TableDataInfo list(Long performId)
    {
        startPage();
        List<SessionVo> list = yanchuSessionService.selectSessionVoList(performId);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(p ->{
                p.setTime(p.getPlayTime().getTime());
            });
        }
        return getDataTable(list);
    }

    @ApiOperation("小程序查询有场次的日期 2023年12月~当前12个月后（验票端）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operaId",value = "当前登录人的operaId",required = true,dataTypeClass = Long.class,dataType = "Long")
    })
    @GetMapping("/dateList/forAllUser")
    public AjaxResult ticketList(Long operaId){
        return AjaxResult.success(yanchuSessionService.dateList(operaId));
    }

    @ApiOperation("小程序查询当月场次（验票端）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "time",value = "当前时间 2023-10-31",required = true,dataTypeClass = String.class,dataType = "String"),
            @ApiImplicitParam(name = "operaId",value = "当前登录人的operaId",required = true,dataTypeClass = Long.class,dataType = "long"),
    })
    @GetMapping("/ticketList/forAllUser")
    public AjaxResult ticketList(String time,Long operaId){
        return AjaxResult.success(yanchuSessionService.ticketList(time,operaId));
    }

    @ApiOperation("小程序获取场次详细信息")
    @GetMapping(value = "/forAllUser/{id}")
    public AjaxResult getInfoForAllUser(@PathVariable("id") Long id)
    {
        return success(yanchuSessionService.selectYanchuSessionById(id));
    }

    /**
     * 导出场次列表
     */
    @PreAuthorize("@ss.hasPermi('system:session:export')")
    @Log(title = "场次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuSession yanchuSession)
    {
        List<YanchuSession> list = yanchuSessionService.selectYanchuSessionList(yanchuSession);
        ExcelUtil<YanchuSession> util = new ExcelUtil<YanchuSession>(YanchuSession.class);
        util.exportExcel(response, list, "场次数据");
    }

    @ApiOperation("获取场次详细信息")
    @PreAuthorize("@ss.hasPermi('system:session:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuSessionService.selectYanchuSessionById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:session:add')")
    @Log(title = "场次", businessType = BusinessType.INSERT)
    @ApiOperation("新增场次")
    @ApiImplicitParam(name = "yanchuSession", value = "场次对象", dataType = "YanchuSession", dataTypeClass = YanchuSession.class)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated YanchuSession yanchuSession)
    {
        return toAjax(yanchuSessionService.insertYanchuSession(yanchuSession));
    }

    @PreAuthorize("@ss.hasPermi('system:session:up')")
    @Log(title = "场次发布", businessType = BusinessType.INSERT)
    @ApiOperation("场次发布")
    @ApiImplicitParam(name = "sessionId", value = "场次对象", dataType = "Long", dataTypeClass = Long.class)
    @PostMapping(value = "/up")
    public AjaxResult up(Long sessionId)
    {
        return toAjax(yanchuSessionService.up(sessionId));
    }

    @ApiOperation("修改场次")
    @PreAuthorize("@ss.hasPermi('system:session:edit')")
    @Log(title = "场次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YanchuSession yanchuSession)
    {
        return toAjax(yanchuSessionService.updateYanchuSession(yanchuSession));
    }
    @ApiOperation("场次停售")
    @ApiImplicitParam(name = "id", value = "场次编码", dataType = "Long", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:session:stop')")
    @Log(title = "场次停售", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/down")
    public AjaxResult down(Long id)
    {
        return toAjax(yanchuSessionService.down(id));
    }

    @ApiOperation("场次取消")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场次编码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "cancelTips", value = "取消原因", dataType = "String", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermi('system:session:cancel')")
    @Log(title = "场次取消", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/cancel")
    public AjaxResult cancel(Long id,String cancelTips)
    {
        return toAjax(yanchuSessionService.cancel(id,cancelTips));
    }

    @ApiOperation("删除场次")
    @PreAuthorize("@ss.hasPermi('system:session:remove')")
    @Log(title = "场次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(yanchuSessionService.deleteYanchuSessionById(id));
    }

    @ApiOperation("复制场次")
    @ApiImplicitParam(name = "yanchuSession", value = "场次对象", dataType = "YanchuSession", dataTypeClass = YanchuSession.class)
    @PostMapping("/copySession")
    public AjaxResult copySession(@RequestBody @Validated YanchuSession yanchuSession){
        return toAjax(yanchuSessionService.copySession(yanchuSession));
    }
}
